//欢迎
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slc_boxes/flutter/slc/common/text_util.dart';
import 'package:flutter_slc_boxes/flutter/slc/res/styles.dart';
import 'package:provider/provider.dart';

import '../../../../generated/l10n.dart';
import '../../../base/api/result_entity.dart';
import '../../../base/startup/task_utils.dart';
import '../../../base/ui/app_mvvm.dart';
import '../../../base/utils/app_toast.dart';
import '../../auth/repository/remote/auth_api.dart';
import '../../../lib/fast/utils/bar_utils.dart';
import '../../../module/user/repository/local/sp_user_config.dart';
import '../../auth/ui/login_page.dart';

class WelcomePage extends AppBaseStatelessWidget<_WelcomeVm> {
  static const String routeName = '/';

  WelcomePage({super.key});

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    // 隐藏状态栏和底部按钮栏
    BarUtils.showEnabledSystemUI(false);
    return ChangeNotifierProvider(
      create: (context) => _WelcomeVm(),
      builder: (context, child) {
        registerEvent(context);
        getVm().init(context);
        return Scaffold(
          body: Column(
            children: [
              const Spacer(flex: 1),
              Expanded(
                  flex: 3,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Center(
                          child: Image(
                              image: AssetImage("assets/images/ic_launcher.png"), width: 56, height: 56)),
                      Text(S.current.app_name, style: Theme.of(context).textTheme.titleMedium)
                    ],
                  )),
              Expanded(
                  flex: 3,
                  child: Center(
                    child: Text(S.current.label_loading,
                        style: SlcStyles.getTextColorHintStyleByTheme(themeData)),
                  )),
              const Spacer(flex: 1),
            ],
          ),
        );
      },
    );
  }
}

class _WelcomeVm extends AppBaseVm {
  final CancelToken cancelToken = CancelToken();

  _WelcomeVm() {
  }

  void init(BuildContext context) async {
    TaskUtils.execOtherTask(context).then((value) async {
      await Future.delayed(const Duration(milliseconds: 1200));
      if (UserConfig().isAutoLogin() &&
          UserConfig().isSavePassword() &&
          !TextUtil.isEmpty(UserConfig().getAccount()) &&
          !TextUtil.isEmpty(UserConfig().getPassword())) {
        showLoading(text: S.current.user_label_logging_in);
        AuthRepository.login(
            UserConfig().getAccount()!, UserConfig().getPassword()!, cancelToken)
            .then((IntensifyEntity<dynamic> value) {
          dismissLoading();
          if (value.isSuccess()) {
            AppToastBridge.showToast(msg: S.current.user_toast_login_login_successful);
            //startByPage(MainPage(), finish: true);
          } else {
            UserConfig().saveIsAutoLogin(true);
            AppToastBridge.showToast(msg: value.getMsg());
            pushReplacementNamed(LoginPage.routeName);
          }
        }, onError: (e) {
          dismissLoading();
          if (!cancelToken.isCancelled) {
            pushReplacementNamed(LoginPage.routeName);
          }
        });
      } else {
        pushReplacementNamed(LoginPage.routeName);
      }
    });
  }

  @override
  void dispose() {
    cancelToken.cancel();
    super.dispose();
  }
}
